package cn.com.zp.util.database;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.SQLException;

public class OracleBlobUtils {
	Connection conn = null;

	public OracleBlobUtils() {
		//conn = DBUtil.getConn();
	}

	@SuppressWarnings("deprecation")
	public void write(String file) throws SQLException, IOException {
		java.sql.Statement stmt = null;
		java.sql.Statement stmt2 = null;
		java.sql.ResultSet rs = null;
		conn.setAutoCommit(false);
		String sql = "insert into image_tab(img_id,image) values(1,empty_blob())";
		stmt = conn.createStatement();
		stmt.executeUpdate(sql);

		String sql2 = "select image from image_tab where img_id = 1 for update";
		stmt2 = conn.createStatement();
		rs = stmt2.executeQuery(sql2);
		if (rs.next()) {
			oracle.sql.BLOB b = (oracle.sql.BLOB) rs.getBlob(1);
			java.io.OutputStream os = b.getBinaryOutputStream();
			java.io.InputStream is = new java.io.FileInputStream(file);
			int i = 0;
			while ((i = is.read()) != -1) {
				os.write(i);
			}
			conn.commit();

			if (is != null) {
				is.close();
			}
			if (os != null) {
				os.close();
			}
			System.out.println("写数据成功!");
		}
	}

	private void read(String file) throws SQLException, IOException {

		java.sql.Statement stmt = null;
		java.sql.ResultSet rs = null;

		String sql = "select image from image_tab where img_id=1 ";
		stmt = conn.createStatement();
		rs = stmt.executeQuery(sql);
		if (rs.next()) {
			oracle.sql.BLOB b = (oracle.sql.BLOB) rs.getBlob(1);
			java.io.InputStream is = b.getBinaryStream();
			java.io.FileOutputStream fos = new java.io.FileOutputStream(file);
			int i = 0;
			while ((i = is.read()) != -1) {
				fos.write(i);
			}
			if (fos != null) {
				fos.close();
			}
			if (is != null) {
				is.close();
			}

		}

	}

	@SuppressWarnings("deprecation")
	private void update(String file) {
		java.sql.Statement stmt = null;
		java.sql.ResultSet rs = null;
		OutputStream os = null;
		InputStream is = null;
		try {
			conn.setAutoCommit(false);
			stmt = conn.createStatement();
			rs = stmt
					.executeQuery("select image from image_tab where img_id=1 for update");

			if (rs.next()) {
				oracle.sql.BLOB b = (oracle.sql.BLOB) rs.getBlob(1);
				os = b.getBinaryOutputStream();
				is = new java.io.FileInputStream(file);
				int i = 0;
				while ((i = is.read()) != -1) {
					os.write(i);
				}
				System.out.println("写数据成功!");
			}
			conn.commit();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (is != null) {
					is.close();
				}
				if (os != null) {
					os.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	public static void main(String args[]) throws SQLException, IOException {
		OracleBlobUtils er = new OracleBlobUtils();
		er.write("D:\\wulihai\\合影.jpg");// 读取文件系统，写blob
		er.read("D:\\wulihai\\合影2.jpg");// 读取blob，写文件系统
		er.update("D:\\wulihai\\写给亲爱的猪.doc");// 读取文件系统，更新blob
		er.read("D:\\wulihai\\写给亲爱的猪2.doc");// 再次读取blob，写文件系统
	}

}
